package com.jiazhong.mingxing.boot.boot15.controller;

import com.alibaba.fastjson.JSONArray;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipayTradePagePayRequest;
import com.alipay.api.request.AlipayTradeQueryRequest;
import com.alipay.api.request.AlipayTradeRefundRequest;
import com.alipay.api.response.AlipayTradeQueryResponse;
import com.alipay.api.response.AlipayTradeRefundResponse;
import com.jiazhong.mingxing.boot.boot15.bean.Alipay;
import lombok.SneakyThrows;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/alipay")
public class AlipayController {

    @Value("${appId}")
    private String appId;
    @Value("${privateKey}")
    private String privateKey;
    @Value("${publicKey}")
    private String publicKey;
    @Value("${notifyUrl}")
    private String notifyUrl;
    @Value("${returnUrl}")
    private String returnUrl;
    @Value("${signType}")
    private String signType;
    @Value("${charset}")
    private String charset;
    @Value("${gatewayUrl}")
    private String gatewayUrl;

    private AlipayClient alipayClient;

    @SneakyThrows
    @RequestMapping("/pay")
    public String alipay(Alipay alipay) {
        // 1. 产生客户端
        alipayClient = new DefaultAlipayClient(gatewayUrl, appId, privateKey,
                "json", charset, publicKey, signType);
        // 2. 产生请求接口并设置响应属性
        AlipayTradePagePayRequest alipayRequest = new AlipayTradePagePayRequest();
        alipayRequest.setReturnUrl(returnUrl);
        alipayRequest.setNotifyUrl(notifyUrl);
        String jsonString = JSONArray.toJSONString(alipay);
        alipayRequest.setBizContent(jsonString);
        // 3. 产生订单
        return alipayClient.pageExecute(alipayRequest).getBody();
    }

    @SneakyThrows
    @RequestMapping("/query")
    public String query(Alipay alipay) {
        // 1. 产生查询请求并设置id
        AlipayTradeQueryRequest queryRequest = new AlipayTradeQueryRequest();
        String jsonString = JSONArray.toJSONString(alipay);
        queryRequest.setBizContent(jsonString);
        // 2. 执行并获取到结果
        AlipayTradeQueryResponse response = alipayClient.execute(queryRequest);
        return response.getTradeStatus();
    }

    @SneakyThrows
    @RequestMapping("/refund")
    public String refund(Alipay alipay) {
        // 1. 产生请求并设置id和退款金额
        AlipayTradeRefundRequest refundRequest = new AlipayTradeRefundRequest();
        String jsonString = JSONArray.toJSONString(alipay);
        refundRequest.setBizContent(jsonString);
        // 2. 执行并获取结果
        AlipayTradeRefundResponse response = alipayClient.execute(refundRequest);
        return response.isSuccess()?"退款成功":"退款失败";
    }

    @GetMapping("/success")
    public String success() {
        return "success";
    }

}
